//
//  HMIAPHelper.m
//  Hangman
//
//  Created by  Stefan Nestorov on 9/27/14.
//  Copyright (c) 2014 Ray Wenderlich. All rights reserved.
//

#import "HMIAPHelper.h"
#import "IAPProduct.h"
#import "Tracker.h"
#import "Globals.h"
#import "CommunicationManager.h"
@import StoreKit;

@implementation HMIAPHelper


- (id)init
{
    
    IAPProduct * offer1 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_1];
    IAPProduct * offer2 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_2];
    IAPProduct * offer3 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_3];
    IAPProduct * offer4 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_4];
//    IAPProduct * offer5 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_5];
//    IAPProduct * offer6 = [[IAPProduct alloc] initWithProductIdentifier: OFFER_6];
    
    NSMutableDictionary * products = [@{ offer1.productIdentifier : offer1,
                                         offer2.productIdentifier : offer2,
                                         offer3.productIdentifier : offer3,
                                         offer4.productIdentifier : offer4 } mutableCopy];
    
    if ( (self = [super initWithProducts:products]) ) {
        
        // Unlock content if its Purchesed
        if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"shouldHideAdView"] == YES ) {
//            IAPProduct * product = self.products[REMOVE_ADS_ID];
//            product.purchase = YES;
        }
        
        if ( [[NSUserDefaults standardUserDefaults] boolForKey:@"isIncreasedStorageSize"] == YES ) {
//            IAPProduct * product = self.products[INCREASE_STORAGE_ID];
//            product.purchase = YES;
        }
        
    }
    
    return self;
}


+ (HMIAPHelper *)sharedInstance
{
    
    static dispatch_once_t once;
    static HMIAPHelper * sharedInstance;
    dispatch_once(&once, ^{
        sharedInstance = [[self alloc] init];
    });
    
    return sharedInstance;
}


#pragma mark - Override IAPHelper methods
- (void)provideContentForProductIdentifier:(NSString *)productIdentifier
{
    
    // Get buyed IAPProduct
    IAPProduct *iapProduct = self.products[productIdentifier];
    NSInteger reward = 0;
    
    // format skProduct price to local price
    NSNumberFormatter *priceFormatter = [[NSNumberFormatter alloc] init];
    [priceFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [priceFormatter setLocale:iapProduct.skProduct.priceLocale];
    NSString *priceString = [priceFormatter stringFromNumber:iapProduct.skProduct.price];
    NSString *purchaseName = @"";
    
    if ( [iapProduct.productIdentifier isEqualToString:OFFER_1] ) {
        
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:IS_PURCHASED_UNLIMITED_ENERGY_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
        
        // If Purchase is Non-Consumable set purchse flag to YES, so it can Not be purchased again
        IAPProduct *product = self.products[OFFER_1];
        product.purchase = YES;
        purchaseName = @"Unlimited Energy";
        
    } else if ( [iapProduct.productIdentifier isEqualToString:OFFER_2] ) {
        
        reward = iapProduct.reward != 0 ? iapProduct.reward : 2000;
        purchaseName = @"S Coins";
    
    } else if ( [iapProduct.productIdentifier isEqualToString:OFFER_3] ){
        
        reward = iapProduct.reward != 0 ? iapProduct.reward : 7000;
        purchaseName = @"M Coins";
        
    } else if ( [iapProduct.productIdentifier isEqualToString:OFFER_4] ){
        
        reward = iapProduct.reward != 0 ? iapProduct.reward : 15000;
        purchaseName = @"XL Coins";
        
    }
//    else if ( [iapProduct.productIdentifier isEqualToString:OFFER_5] ){
//        
//        reward = iapProduct.reward != 0 ? iapProduct.reward : 17500;
//        purchaseName = @"L Coins";
//        
//    } else if ( [iapProduct.productIdentifier isEqualToString:OFFER_6] ){
//        
//        reward = iapProduct.reward != 0 ? iapProduct.reward : 20000;
//        purchaseName = @"XL Coins";
//        
//    }
    
    // Track Consumable Purchase
    NSString *eventName = [NSString stringWithFormat:@"Bought %@", purchaseName];
    [Tracker trackEvent:eventName withParams:@{
                                               @"Price" : priceString ?: @"0",
                                               @"Reward" : @(reward) ?: @"No Reward",
                                               }];
    
    // Update user money on the server
    if ( reward > 0 ) {
        [[CommunicationManager sharedManager] updateMyselfMoney:reward];
    }
    
}


@end

